import { defineStore } from "pinia";
import {getCurrentGlobalProperties,  store} from "@frontend/vue-common";
import {
    listCounterpartyContactAPI,
    addCounterpartyContactAPI,
    getCounterpartyContactAPI,
    editCounterpartyContactAPI,
    deleteCounterpartyContactAPI,
} from "@/api/counterpartyContact";


const filter = class {
    constructor() {
        this.counterpartyId = {value: null}
		this.contactName = {value: null}
		this.contactPhone = {value: null}
		this.contactAddress = {value: null}
		this.provinceName = {value: null}
		this.provinceCode = {value: null}
		this.cityName = {value: null}
		this.cityCode = {value: null}
		this.areaName = {value: null}
		this.areaCode = {value: null}
		
    }
}

const form = class {
    constructor() {
        this.id = null
        this.counterpartyId = null
		this.contactName = null
		this.contactPhone = null
		this.contactAddress = null
		this.provinceName = null
		this.provinceCode = null
		this.cityName = null
		this.cityCode = null
		this.areaName = null
		this.areaCode = null
		
    }
}

const newStore = defineStore({
    id: "counterpartyContact",
    state: () => ({
        list: [],
        pageNum: 1,
        pageSize: 20,
        totalCount: 0,
        sortField: "id",
        sortOrder: -1,
        filter: new filter,
        form: new form,
        statusOptions: [{code: 1, name: "待确认"}, {code: 2, name: "已上线"}, {code: 3, name: "待删除"},{code:4, name: "已删除"}],
        selected: [],
        dataList: [],
    }),
    actions: {
        cleanAll() {
            this.list = []
            this.pageNum = 1
            this.pageSize = 20
            this.totalCount = 0
            this.sortField = "id"
            this.sortOrder = -1
            this.filter = new filter
            this.form = new form
        },
        cleanFilter() {
            this.filter = new filter
        },
        cleanForm() {
            this.form = new form
        },
        async listCounterpartyContact() {
            return new Promise((resolve, reject) => {
                listCounterpartyContactAPI(this.pageNum, this.pageSize, this.sortField, this.sortOrder, {
                    counterpartyId: this.filter.counterpartyId.value,
					contactName: this.filter.contactName.value,
					contactPhone: this.filter.contactPhone.value,
					contactAddress: this.filter.contactAddress.value,
					provinceName: this.filter.provinceName.value,
					provinceCode: this.filter.provinceCode.value,
					cityName: this.filter.cityName.value,
					cityCode: this.filter.cityCode.value,
					areaName: this.filter.areaName.value,
					areaCode: this.filter.areaCode.value,
					
                })
                    .then((res) => {
                        this.list = res.data
                        this.totalCount = res.total
                        resolve()
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        async addCounterpartyContact(data) {
            return new Promise((resolve, reject) => {
                addCounterpartyContactAPI(data)
                    .then(() => {
                        this.listCounterpartyContact()
                            .then(()=>{
                                getCurrentGlobalProperties().$toast.add({severity: 'success', summary: '操作成功', detail: '', life: 2000});
                                resolve()
                            })
                            .catch(err => {
                                reject(err);
                            });
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        async editCounterpartyContact() {
            return new Promise((resolve, reject) => {
                editCounterpartyContactAPI(this.form)
                    .then(() => {
                        this.listCounterpartyContact()
                            .then(()=>{
                                getCurrentGlobalProperties().$toast.add({severity: 'success', summary: '操作成功', detail: '', life: 2000});
                                resolve()
                            })
                            .catch(err => {
                                reject(err);
                            });
                    })
                    .catch(err => {
                        reject(err)
                    })
            })
        },
        async getCounterpartyContact(id) {
            return new Promise((resolve, reject) => {
                getCounterpartyContactAPI({
                    id: id,
                })
                    .then((res) => {
                        this.form = res.data
                        resolve()
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
        async deleteCounterpartyContact(ids) {
            return new Promise((resolve, reject) => {
                deleteCounterpartyContactAPI(ids)
                    .then(() => {
                        this.listCounterpartyContact()
                            .then(()=>{
                                getCurrentGlobalProperties().$toast.add({severity: 'success', summary: '操作成功', detail: '', life: 2000});
                                resolve()
                            })
                            .catch(err => {
                                reject(err);
                            });
                    })
                    .catch(err => {
                        reject(err);
                    });
            });
        },
    }
})

export default newStore(store)
